<?php
declare (strict_types=1);

namespace app\common\library;

use app\common\exception\LogicException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

// use think\facade\Db;

/**
 *
 */
class Queue
{
    /**
     * push
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/08/07
     *
     * @return void
     */
    public static function push(string $method, array $params, int $delayed = 0)
    {
        $client = new Client([
            'base_uri' => 'http://' . env('app.queue_name') . ':9501',
            'timeout'  => 2.0
        ]);
        $opts   = [
            'json' => [
                'jsonrpc' => '2.0',
                'method'  => '/queue/push',
                'params'  => [$method, $params, $delayed],
                'id'      => microtime()
            ]
        ];
        try {
            $response = $client->post('/', $opts);
            $ret      = $response->getBody()->getContents();
            $res      = json_decode($ret, true);
            // if ($res['result'] !== true) {
            //     // Db::name('test')->save(['msg' => $ret]);
            // }
        } catch (RequestException $exception) {
            throw new LogicException($exception->getCode(), $exception->getMessage());
        }
    }
}